package com.example.moshudriver.activity;

import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.BeeFramework.model.BusinessResponse;
import com.sgr.dykdriver.R;
import com.example.moshudriver.adapter.CouponListAdapter;
import com.example.moshudriver.adapter.MyIncometAdapter;
import com.example.moshudriver.model.DrawModel;
import com.example.moshudriver.model.UserBalanceModel;
import com.example.moshudriver.protocol.ApiInterface;
import com.example.moshudriver.protocol.WITHDRAW_ORDER;
import com.example.moshudriver.protocol.orderlistpublishedResponse;
import com.external.androidquery.callback.AjaxStatus;
import com.external.maxwin.view.IXListViewListener;
import com.external.maxwin.view.XListView;

public class MyIncomeActivity extends Activity implements OnClickListener,
		BusinessResponse, IXListViewListener {

	private XListView myincome_xlist;
	private ImageView mEmptyView;
	private MyIncometAdapter incomeAdapter;
	TextView top_view_title,myincome_count;
	EditText recharge_edit;
	ImageView top_view_back;
	UserBalanceModel withdrawModel;
	DrawModel withdraw_list;
	public ArrayList dataList = new ArrayList();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_myincome);

		initView();
		withdraw_list = new DrawModel(this);
		withdraw_list.addResponseListener(this);
		withdraw_list.getList_draw();
	
	}

	private void initView() {
		myincome_count=(TextView) findViewById(R.id.myincome_count);
		top_view_title = (TextView) findViewById(R.id.top_view_title);
		top_view_title.setText(R.string.my_income);
		recharge_edit = (EditText) findViewById(R.id.recharge_edit);
		top_view_back = (ImageView) findViewById(R.id.top_view_back);
		top_view_back.setOnClickListener(this);
		mEmptyView = (ImageView) findViewById(R.id.e0_empty_view);
		mEmptyView.setVisibility(View.GONE);
		
		myincome_xlist = (XListView) findViewById(R.id.myincome_xlist);
		myincome_xlist
				.setOnItemClickListener(new AdapterView.OnItemClickListener() {
					@Override
					public void onItemClick(AdapterView<?> parent, View view,
							int position, long id) {

					}
				});
		myincome_xlist.setPullRefreshEnable(true);
		myincome_xlist.setPullLoadEnable(true);
		myincome_xlist.setXListViewListener(this, 2);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
	

		case R.id.top_view_back:
			finish();
			break;
		}

	}

	@Override
	public void OnMessageResponse(String url, JSONObject jo, AjaxStatus status)
			throws JSONException {
		myincome_xlist.stopRefresh();
		myincome_xlist.stopLoadMore();
		if (url.endsWith(ApiInterface.WITHDRAW_DRAW)) {

			if (null != jo) {
				myincome_xlist.loadMoreShow();
				mEmptyView.setVisibility(View.GONE);
				myincome_xlist.setVisibility(View.VISIBLE);
				
				if (null == incomeAdapter) {
					incomeAdapter = new MyIncometAdapter(
							MyIncomeActivity.this, withdraw_list.publicWithdrawOrders);
					myincome_xlist.setAdapter(incomeAdapter);
					
					
					
				} else {
					incomeAdapter.publicList=withdraw_list.publicWithdrawOrders;
					incomeAdapter.notifyDataSetChanged();
				}

				orderlistpublishedResponse response = new orderlistpublishedResponse();
				response.fromJson(jo);
				java.text.DecimalFormat   df   =new   java.text.DecimalFormat("#.00");  

				myincome_count.setText("总收益"+String.valueOf(df.format(Count(withdraw_list.publicWithdrawOrders)))+"元");
				
				if (0 == response.more) {
					myincome_xlist.stopLoadMore();
					myincome_xlist.setPullLoadEnable(false);
				} else {
					myincome_xlist.stopLoadMore();
					myincome_xlist.setPullLoadEnable(true);
				}
			} else {
				if (withdraw_list.couponList.size() == 0) {
					mEmptyView.setVisibility(View.VISIBLE);
					myincome_xlist.setVisibility(View.GONE);
				}
			}
		}

	}

	@Override
	public void onRefresh(int id) {
		withdraw_list.getList_draw();
	}

	@Override
	public void onLoadMore(int id) {
		withdraw_list.getList_draw_more();
//		Toast.makeText(this, "onLoadMore", 2000).show();

	}
	//计算总收益
	public double Count(ArrayList<WITHDRAW_ORDER> publicWithdrawOrders){
		int length=publicWithdrawOrders.size();
		double sum=0;
		for(int i=0;i<length;i++){
			sum=sum+Double.parseDouble(publicWithdrawOrders.get(i).amount);
		}
		return sum;
		
	}

}